package com.qupai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qupai.constant.MessageConstant;
import com.qupai.dto.PtRegistrationAddDTO;
import com.qupai.dto.PtRegistrationDeleteDTO;
import com.qupai.entity.PtActivity;
import com.qupai.entity.PtRegistration;
import com.qupai.entity.User;
import com.qupai.exception.BaseException;
import com.qupai.mapper.PtRegistrationMapper;
import com.qupai.mapper.UserMapper;
import com.qupai.service.PtRegistrationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class PtRegistrationServiceImpl extends ServiceImpl<PtRegistrationMapper, PtRegistration> implements PtRegistrationService {

    private final PtRegistrationMapper registrationMapper;
    private final UserMapper userMapper;

    /*
    * 获取活动报名人员名单
    * */
    @Override
    public List<User> getRegistrationsByActivityId(Long activityId) {
        //先查询活动报名记录
        QueryWrapper<PtRegistration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId);
        List<PtRegistration> registrations = registrationMapper.selectList(queryWrapper);

        //根据记录查询User数据
        List<User> signUpUser = new ArrayList<>();
        for (PtRegistration registration : registrations) {
            signUpUser.add(userMapper.selectById(registration.getUserId()));
        }

        return  signUpUser;
    }

    @Override
    public void deleteUser(PtRegistrationDeleteDTO registrationDeleteDTO) {

        QueryWrapper<PtRegistration>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(PtRegistration::getActivityId,registrationDeleteDTO.getActivityId())
                .in(PtRegistration::getUserId,registrationDeleteDTO.getUserIds());
        registrationMapper.delete(queryWrapper);
    }

    @Override
    public void addByStudentId(PtRegistrationAddDTO registrationAddDTO) {
        //跟根据学号获取用户的跟人信息
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper.eq("student_id",registrationAddDTO.getStudentId());
        List<User> userList = userMapper.selectList(userWrapper);

        //查询是否存在此学号的学生
        User user=new User();
        if (userList != null && !userList.isEmpty()) {
            user = (User) userList.get(0);
        }else{
            throw new BaseException(MessageConstant.StudentIdNotExist);
        }

        //判断当前学生是否已经报名
        PtActivity activity =registrationAddDTO.getActivity();
        //获取报名名单
        QueryWrapper<PtRegistration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activity.getId())
                .eq("user_student_id", user.getStudentId());
        List<PtRegistration> registrations = registrationMapper.selectList(queryWrapper);
        //如果存在则报异常
        if(registrations!=null && !registrations.isEmpty()){
            throw new BaseException(MessageConstant.StudentHasExist);
        }


        PtRegistration registration = PtRegistration.builder()
                .activityId(activity.getId())
                .activityImage(activity.getImage())
                .activityCoins(activity.getActivityCoins())
                .activity_organizationsId(activity.getOrganizationsId())
                .userId(user.getId())
                .userName(user.getName())
                .userPhone(user.getPhone())
                .userStudentId(user.getStudentId())
                .userCollegesId(user.getCollegesId())
                .userClassName(user.getClassName())
                .userMajor(user.getMajor())
                .signUpTime(LocalDateTime.now())
                .build();

        registrationMapper.insert(registration);
    }
}
